package com.tr.demo01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author TRasdzz
 * @since 2021/3/8 16:32
 * @version 1.0
 */

//练习多线程，实现多线程同步下载图片

public class TestThread02 implements Runnable {
    //网络图片地址
    private String url;
    //保存的文件名
    private String name;

    public TestThread02(String url, String name){
        this.url = url;
        this.name = name;
    }
    @Override
    public void run() {
        WebDownLoad webDownLoad = new WebDownLoad();
        webDownLoad.downLoader(url,name);
        System.out.println("下载了名为" + name + "的图片");
    }

    public static void main(String[] args) {
        TestThread02 t1 = new TestThread02("http://i0.hdslb" +
                ".com/bfs/space/768cc4fd97618cf589d23c2711a1d1a729f42235.png",
                "E:\\学习\\Java\\代码相关\\多线程\\src\\com\\tr\\demo01\\bilibili,jpg");
        TestThread02 t2 = new TestThread02("http://i0.hdslb.com/bfs/album" +
                "/c3c703eb8740ac7949e6d2c0213b5786a83220c3.jpg@518w.jpg", "E:\\学习\\Java" +
                "\\代码相关\\多线程\\src\\com\\tr\\demo01\\冰冰.jpg");
        TestThread02 t3 = new TestThread02("http://i0.hdslb" +
                ".com/bfs/space/cb1c3ef50e22b6096fde67febe863494caefebad.png", "E" +
                ":\\学习\\Java\\代码相关\\多线程\\src\\com\\tr\\demo01\\space" +
                ".jpg");
        new Thread(t1).start();
        new Thread(t2).start();
        new Thread(t3).start();
    }
}


class WebDownLoad {
    public void downLoader(String url, String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
